/**
 * 
 */
package soa.security.test.ws;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.junit.Test;

/**
 * @author Cre.Gu
 * 
 */
public class PositionServiceClient extends WebServiceClient {
	protected static final org.slf4j.Logger log = org.slf4j.LoggerFactory
			.getLogger(PositionServiceClient.class);

	public static final String WSURL = BASE_WSDL + "PositionService?wsdl";

	public Boolean addPosition(String organizationId, String position) {
		try {
			String method = "addPosition";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName);

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("position", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			Object obj = call.invoke(new Object[] { organizationId, position });
			Boolean retData = (Boolean) obj;
			return retData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public Boolean updatePosition(String organizationId, String position) {
		try {
			String method = "updatePosition";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName);

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("position", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			Object obj = call.invoke(new Object[] { organizationId, position });
			Boolean retData = (Boolean) obj;
			return retData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String allPositions(String organizationId) {
		try {
			String method = "allPositions";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName);

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);

			Object obj = call.invoke(new Object[] { organizationId });
			String retData = (String) obj;
			return retData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public String getPosition(String organizationId, String positionId) {
		try {
			String method = "getPosition";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName);

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("positionId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);

			return (String) call.invoke(new Object[] { organizationId,
					positionId });
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public Boolean addPersons(String organizationId, String positionId,
			String personIds) {
		try {
			String method = "addPersons";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("positionId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("personIds", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { organizationId,
					positionId, personIds });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public Boolean removePersons(String organizationId, String positionId,
			String personIds) {
		try {
			String method = "removePersons";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("positionId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("personIds", XMLType.XSD_STRING, ParameterMode.IN);
			call.setReturnType(XMLType.XSD_BOOLEAN);

			return (Boolean) call.invoke(new Object[] { organizationId,
					positionId, personIds });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	public String allPersons(String organizationId, String positionId) {
		try {
			String method = "allPersons";
			Service service = new Service();

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(WSURL));

			QName qName = new QName(NAMESPACE, method);
			call.setOperationName(qName); // 调用方法

			call.addParameter("organizationId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.addParameter("positionId", XMLType.XSD_STRING,
					ParameterMode.IN);
			call.setReturnType(XMLType.XSD_STRING);

			return (String) call.invoke(new Object[] { organizationId,
					positionId });
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	@Test
	public void testAdd() throws Exception {
		addPosition(ORGANIZATIONID, WebServiceClient.readXml("position.xml"));
	}

	@Test
	public void testUpdate() throws Exception {
		updatePosition(ORGANIZATIONID, WebServiceClient.readXml("position.xml"));
	}

	@Test
	public void testOther() throws Exception {

		String all = allPositions(ORGANIZATIONID);
		System.out.println(all);

		String get = getPosition(ORGANIZATIONID, "PO0001");
		System.out.println(get);
	}
	
	@Test
	public void testRel() throws Exception {
		addPersons(ORGANIZATIONID, "PO0001", "PE0001,PE0002");
		System.out.println(allPersons(ORGANIZATIONID,"PO0001"));
		
		removePersons(ORGANIZATIONID, "PO0001", "PE0001");
		System.out.println(allPersons(ORGANIZATIONID,"PO0001"));
	}
}
